<template>
  <div>
    <!-- 搜索 -->
    <el-form :inline="true"  class="demo-form-inline">
      <el-form-item label="关键字:">
        <el-input placeholder="输入关键字" v-model="params.keyWord"></el-input>
      </el-form-item>
      <el-form-item label="开始时间:">
        <el-date-picker
          type="date"
          v-model="params.start"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间:">
        <el-date-picker
          type="date"
          v-model="params.end"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getItemLists" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
    <!-- /搜索 -->
    <!-- 增加按钮导出excel -->
    <div class="action-btns">
      <el-button @click="$router.push('/itemAdd')" type="warning" size="mini">增加商品</el-button>
      <download-excel
        :data="items"
        :fields="fields"
        name="商品"
      >
        <el-button type="danger" size="mini" icon="el-icon-download">导出</el-button>
      </download-excel>
    </div>
    <!-- 表格显示分类列表数据 -->
    <el-table
      :data="items"
      style="width: 100%">
      <el-table-column
        prop="itemName"
        label="商品名称">
      </el-table-column>
      <el-table-column
        prop="desc"
        label="商品描述">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark"  placement="bottom">
            <div slot="content" style="max-width: 400px">{{scope.row.desc}}</div>
            <div>
              {{scope.row.desc.length > 10 ? scope.row.desc.substr(0, 10) + '...': scope.row.desc}}
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column
        prop="price"
        label="商品价格">
      </el-table-column>
      <el-table-column
        prop="sale"
        label="销量">
         <template slot-scope="scope">
           <el-tag :type="scope.row.sale>500?'danger':'info'">{{scope.row.sale}}</el-tag>
         </template>
      </el-table-column>
      <el-table-column
        prop="onSale"
        label="上下架">
        <template slot-scope="scope">
           <span>{{scope.row.onSale === 0?'上架': '下架'}}</span>
            <el-switch
              :value="scope.row.onSale === 0? true: false"
            />
         </template>
      </el-table-column>
      <el-table-column
        prop="store"
        label="库存">
      </el-table-column>
      <el-table-column
        prop="createAt"
        label="上架时间">
        <template slot-scope="scope">
          {{dayjs(scope.row.createAt).format('YYYY-MM-DD')}}
        </template>
      </el-table-column>
      <el-table-column
        prop="thumb"
        label="封面">
        <template slot-scope="scope">
          <img width="50" :src="scope.row.thumb" alt="">
        </template>
      </el-table-column>
      <el-table-column
        :width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="enterItemUpdate(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="delItem(scope.row.id)" size="mini">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格显示分类列表数据 -->
    <!-- 分页组件 -->
    <el-pagination
      layout="prev, pager, next, jumper, sizes"
      :page-size="params.pageSize"
      :total="total"
      :current-page="params.page"
      @size-change="sizeChange"
      @current-change="currentChange"
      >
    </el-pagination>
    <!-- /分页组件 -->
  </div>
</template>

<script>
import { getItemLists, delItem } from '_api'
import dayjs from 'dayjs'
export default {
  name: 'CateLists',
  data () {
    return {
      items: [],
      // 商品分类请求的参数
      total: 0, // 总条数
      params: {
        keyWord: '',
        start: '',
        end: '',
        page: 1,
        pageSize: 10
      },
      fields: {
        // 导出excel的表头
        序号: 'id',
        商品名: 'itemName',
        商品价格: 'price',
        库存: 'store',
        描述: 'desc',
        封面地址: 'thumb'
      }
    }
  },
  mounted () {
   
  },
  methods: {
    delItem (id) {
      // 删除商品
      this.$messageBox.confirm('此操作将彻底删除该商品，继续吗?', '注意').then(res => {
        delItem(id).then(res => {
          if (res.data.code === 200) {
            this.$message.success({
              message: res.data.msg,
              duration: 1000,
              onClose: () => {
                this.$router.go(0)
              }
            })
          }
        })
      })
    },
    enterItemUpdate (id) {
      // 跳转到修改商品路由
      this.$router.push({
        path: '/itemUpdate',
        query: {
          id
        }
      })
    },
    dayjs,
    sizeChange (size) {
      // 分页改变一页显示多少条
      this.params.pageSize = size
      this.getItemLists()
    },
    currentChange (page) {
      // 分页组件改变页码
      this.params.page = page
      this.getItemLists()
    },
    getItemLists () {
      // 获取商品分类列表数据
      getItemLists(this.params).then(res => {
        if (res.data.code === 200) {
          this.items = res.data.data.list
          this.total = res.data.data.total
          this.params = {
            keyWord: '',
            start: '',
            end: '',
            page: 1,
            pageSize: 10
          }
        }
      })
    }
  },
  created () {
    this.getItemLists()
  }
}
</script>

<style lang="scss" scoped>
.action-btns{
  margin-bottom: 10px;
  display: flex;
}
</style>
